<script setup lang="ts">
  import { ref } from 'vue';
  import { getSecondContractRecord } from '/@/apis/userApi';
  import { useUserId } from '/@/hooks/index';
  import { useRequest } from 'vue-request';
  const { getUserId } = useUserId();
  const userId = getUserId();
  const tradeHistoryList = ref<{ [xx: string]: any }[]>([]);
  const params = ref({
    member: userId,
    type: 1,
  });
  const imgsmap = JSON.parse(localStorage.getItem('tokensImgsMap') as string);
  useRequest(getSecondContractRecord, {
    defaultParams: [params.value],
    onSuccess(data: any) {
      tradeHistoryList.value = data;
    },
  });
  function returnName(name: string) {
    let nameArr = ['CL', 'FCPO', 'GC', 'HG', 'NG', 'OIL', 'XAG', 'XAU', 'XPD', 'XPT'];
    let nameBool = nameArr.some((name1) => {
      return name1 == name;
    });
    if (nameBool) {
      return name + '/USD';
    }
    return name;
  }
</script>

<template>
  <div class="trade_history">
    <div class="order_list_th">
      <div class="width1">
        <p>{{ $t('Currency') }}</p>
        <p>{{ $t('time') }}</p>
      </div>
      <p class="width2">{{ $t('Amount') }}</p>
      <div class="width3">
        <p> {{ $t('purchasePrice') }}</p>
        <p>{{ $t('Markprice') }}</p>
      </div>
      <p class="width4">{{ $t('Profit') }}</p>
    </div>
    <div class="rank_box_list">
      <div v-for="item in tradeHistoryList" :key="item.id" class="rank_box_li">
        <div class="width1 flex_center">
          <div class="img_box" v-if="item.flag == 0">
            <div class="flag flag1">
              <img :src="$img(`country/${item.pairsName.split('/')[0]}.png`)" alt="" />
            </div>
            <div class="flag flag2">
              <img :src="$img(`country/${item.pairsName.split('/')[1]}.png`)" alt="" />
            </div>
          </div>
          <div class="img_box" v-else-if="item.flag == 1">
            <div class="flag flag1">
              <img
                :src="`https://s2.coinmarketcap.com/static/img/coins/64x64/${
                  imgsmap[item.pairsName.split('/')[0]]
                }.png`"
                alt=""
              />
            </div>
            <div class="flag flag2">
              <img :src="`https://s2.coinmarketcap.com/static/img/coins/64x64/825.png`" alt="" />
            </div>
          </div>
          <div class="img_box" v-else-if="item.flag == 2">
            <img :src="`/${item.newPairsName}.png`" />
          </div>
          <div>
            <p class="rank_box_text">{{ returnName(item.newPairsName) }}</p>
            <p>{{ item.seconds }}{{ item.secondsTime }}</p>
          </div>
        </div>
        <div class="width2 flex_arrow">
          <div class="arrow" :class="item.buyStatus == 1 ? 'font_green' : 'font_red'">
            <span v-if="item.buyStatus == 1">↑</span>
            <span v-else>↓</span>
          </div>
          <div>
            {{ item.amount }}
          </div>
        </div>
        <div class="width3">
          <div>{{ item.startAmount }}</div>
          <div>{{ item.finishsAmount }}</div>
        </div>
        <div class="width4">
          <span
            :class="
              item.settleStatus != 2 &&
              (item.isWin === 1 ? 'font_green' : item.isWin === 2 ? 'font_red' : '')
            "
          >
            <span v-if="item.settleStatus != 2">
              <span v-if="item.isWin === 2">-</span> <span v-if="item.isWin === 1">+</span>
            </span>
            {{ item.settleStatus != 2 ? item.profit : $t('Waiting') }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
  .trade_history {
    background-color: $body;
    .order_list_th {
      @include align-center;
      background: #242424;
      height: 72px;
      font-size: 20px;
      padding: 0 30px;
      color: rgb(102, 102, 102);
    }
    .rank_box_list {
      margin: 0 30px;
    }
    .rank_box_li {
      height: 120px;
      @include align-center;
      border-bottom: 1px solid  rgba(255, 255, 255, 0.15);
      font-size: 22px;
      .img_box {
        width: 56px;
        height: 56px;
        background: #242424;
        border-radius: 50%;
        margin-right: 10px;
        position: relative;
        .flag {
          width: 24px;
          height: 24px;
          position: absolute;
        }
        .flag1 {
          top: 12px;
          left: 10px;
        }
        .flag2 {
          top: 20px;
          left: 22px;
        }
      }

      .flex_center {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        & > div:nth-child(2) {
          flex: 1;
        }
      }
    }
    .width1 {
      width: 30%;
    }
    .width2 {
      width: 25%;
      text-align: center;
    }
    .width3 {
      width: 25%;
      text-align: center;
    }
    .width4 {
      width: 20%;
      text-align: right;
    }
    .font_red {
      color: $primary-red;
      font-weight: bold;
      font-size: 20px;
    }
    .font_green {
      color: $primary-green;
      font-weight: bold;
      font-size: 20px;
    }
    .flex_arrow {
      @include center;
      .arrow {
        margin-right: 2px;
        font-size: 26px;
      }
    }
  }
</style>
